package com.beadhouse.nurrecord.support.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.beadhouse.common.dto.QueryResult;
import com.beadhouse.entity.CustomerEntity;
import com.beadhouse.entity.NurLevelEntity;
import com.beadhouse.nurrecord.dao.CustomerListDao;
import com.beadhouse.nurrecord.dto.CustomerListDto;
import com.beadhouse.nurrecord.service.CustomerListService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.xml.internal.ws.util.xml.CDATA;

@Service
public class CustomerListServiceImpl implements CustomerListService{
	final CustomerListDao customerDao;
	public CustomerListServiceImpl(CustomerListDao customerDao) {
		this.customerDao=customerDao;
	}
	

	@Override
	public QueryResult<Map<String, Object>> selectAllCustomer(Integer pageNo,Integer pageSize,String key) {
		//1验证参数
		//2执行业务逻辑
		
		PageHelper.startPage(pageNo, pageSize);
		List<CustomerEntity> ce= customerDao.selectAll(key);
		
		PageInfo<CustomerEntity> page = new PageInfo<>(ce);
		List<Map<String, Object>> dtos = new ArrayList<>(ce.size());
		//3返回结果
		for (CustomerEntity centity : ce) {
			dtos.add(entity2map(centity));
		}
		QueryResult<Map<String, Object>> result = new QueryResult<>();
		result.setPageNo(pageNo);
		result.setPageSize(pageSize);
		result.setTotalRows((int)page.getTotal());
		result.setRows(dtos);
		result.setTotalPages(result.getTotalPages());
		return result;
	}
	public Map<String,Object> entity2map(CustomerEntity ce) {
		Map<String,Object> dto = new HashMap<>();
		dto.put("bed", ce.getBedId());
		dto.put("customerAge", ce.getCustomerAge());
		dto.put("customerName", ce.getCustomerName());
		dto.put("customerSex", ce.getCustomerSex());
		NurLevelEntity nurLevel= customerDao.selectById(ce.getId());
		if(nurLevel!=null) {
			dto.put("nurLevelName", nurLevel.getLevelName());
			dto.put("nurLevelId", nurLevel.getId());
		}
		dto.put("customerId", ce.getId());
		return dto;
	}
	
}
